package com.example.springboot10_19.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean( DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
        Map<String,String> fiterMap=new LinkedHashMap<>();
        fiterMap.put("/user/normal","perms[normal]");
        fiterMap.put("/user/vip","perms[vip]");
        fiterMap.put("/user/admin","perms[admin]");
        bean.setFilterChainDefinitionMap(fiterMap);
        bean.setLoginUrl("/login");
        bean.setUnauthorizedUrl("/noauth");
       bean.setSecurityManager(defaultWebSecurityManager(userRealm()));
        bean.setSecurityManager(securityManager);
        return bean;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }
    @Bean
    public  UserRealm userRealm(){
        return new UserRealm();
    }
}
